﻿using Common.Contract.Authentication.Record;
using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request.CustomerManager;
using Common.Contract.Customer.Response.CustomerManager;
using Common.Service.Command;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Customer.Core.ServiceCommand.CustomerManager
{
    public class RetrieveListBranchServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveListBranchRequest, RetrieveListBranchResponse>
    {
        #region Properties
        public UserRecord Dao;
        #endregion
        public RetrieveListBranchServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveListBranchRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.Dao == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.Dao;
            }
        }

        protected override bool OnExecute(RetrieveListBranchResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            List<BranchRecord> records = new List<BranchRecord>();

            List<Branch> listCustomers = Providers.Branchs.RetrieveBranchByDao(Dao);
            records = listCustomers.Select(x => new BranchRecord
            {
                BranchId = x.BranchId,
                BranchName = x.BranchNameSme,
                Region = x.Region,
                BranchCodeSme = x.BranchCodeSme
            }).OrderBy(x => x.BranchId).ToList();

            response.Records = records;

            return isSuccess;
        }


    }
}
